package com.parkingspace.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parkingspace.dao.*;
import com.parkingspace.entity.*;
import com.parkingspace.service.IOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;


/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-05-24
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Resource
    private OrderMapper orderMapper;

    @Resource
    private ParkingInformationMapper parkingInformationMapper;

    @Resource
    private CarMapper carMapper;

    @Resource
    private WalletUserMapper walletUserMapper;

    @Resource
    private PersonWalletMapper personWalletMapper;

    @Override
    public Integer createOrder(Long parkingId, Integer time, Long uid, Long carid, Integer rent) {
        LocalDateTime now = LocalDateTimeUtil.now();
        LocalDateTime plushours = now.plusHours(time);

        System.out.println("plushours = " + plushours);
        System.out.println("now = " + now);
        Order order = new Order();
        order.setParkingId(parkingId);
        order.setCarid(carid);
        order.setCreatTime(Date.from(now.atZone(ZoneId.systemDefault()).toInstant()));
        order.setEndTime(Date.from(plushours.atZone(ZoneId.systemDefault()).toInstant()));
        order.setPrice((double) (time*rent));
        order.setTime(Long.valueOf(time));
        order.setStatus(0);
        int row = orderMapper.insert(order);

        return row;
    }

    @Override
    public List<OrderPlus> getOrder(Long uid, Integer pageNo, Integer pageSize, String address) {
        List<OrderPlus> orderPluses = new ArrayList<>();

        //查出该车主的车辆
        Map<String,Object> carmap = new HashMap<>();
        carmap.put("uid",uid);
        List<Car> carList = carMapper.selectByMap(carmap);
        List<Long> caridList = new ArrayList<>();
        for (Car car : carList) {
            caridList.add(car.getCarid());
        }
        if (StringUtils.isEmpty(address)){
           QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
           orderQueryWrapper.in("carid",caridList);
           Page<Order> orderPage = orderMapper.selectPage(new Page<Order>(pageNo, pageSize), orderQueryWrapper);
           List<Order> orders = orderPage.getRecords();
           //遍历订单集合，生成订单中每个车位地址和每辆车的集合
           List<ParkingInformation> parkings = new ArrayList<>();
           List<Car> cars = new ArrayList<>();
           for (Order o : orders) {
               ParkingInformation parkingInformation = parkingInformationMapper.selectById(o.getParkingId());
               Car car = carMapper.selectById(o.getCarid());
               orderPluses.add(new OrderPlus(o,car,parkingInformation,orderPage));
           }
        }

        if (StringUtils.isNotEmpty(address)){
            //根据查询条件查询出关于车主的所有订单
            List<Long> parkingidList = new ArrayList<>();
            QueryWrapper<ParkingInformation> parkingInformationQueryWrapper = new QueryWrapper<>();
            parkingInformationQueryWrapper.like("parking_address",address);
            List<ParkingInformation> parkingInformations = parkingInformationMapper.selectList(parkingInformationQueryWrapper);
            for (ParkingInformation parkingInformation : parkingInformations) {
                Long parkingId = parkingInformation.getParkingId();
                parkingidList.add(parkingId);
            }

            QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
            orderQueryWrapper.in("carid",caridList).in("parking_id",parkingidList);
            Page<Order> orderPage = orderMapper.selectPage(new Page<Order>(pageNo, pageSize), orderQueryWrapper);
            List<Order> orders = orderPage.getRecords();
            //遍历订单集合，生成订单中每个车位地址和每辆车的集合
            List<ParkingInformation> parkings = new ArrayList<>();
            List<Car> cars = new ArrayList<>();
            for (Order o : orders) {
                ParkingInformation parkingInformation = parkingInformationMapper.selectById(o.getParkingId());
                Car car = carMapper.selectById(o.getCarid());
                orderPluses.add(new OrderPlus(o,car,parkingInformation,orderPage));
            }
        }
        return orderPluses;
    }

    @Override
    public Boolean buyForOrder(Long uid, Long orderid, Double price) {
        Map<String,Object> map = new HashMap<>();
        map.put("uid",uid);
        map.put("rid",3);
        List<WalletUser> walletUsers = walletUserMapper.selectByMap(map);
        Long walletId = walletUsers.get(0).getWalletId();
        PersonWallet personWallet = personWalletMapper.selectById(walletId);
        Double money = personWallet.getMoney();
        if (money<price){
            return false;
        }else{
            personWallet.setMoney(money-price);
            int row = personWalletMapper.updateById(personWallet);
            Order order = new Order();
            order.setOrderid(orderid);
            order.setStatus(1);
            orderMapper.updateById(order);
            return true;
        }
    }
}
